package com.ruoyi.safety.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.control.domain.RyssHistory;
import com.ruoyi.control.domain.SgcdssHistory;
import com.ruoyi.control.mapper.RyssHistoryMapper;
import com.ruoyi.control.mapper.SgcdssHistoryMapper;
import com.ruoyi.control.water.domain.Jingxia;
import com.ruoyi.control.water.domain.Jingxiafenzhan;
import com.ruoyi.control.water.mapper.JingxiaMapper;
import com.ruoyi.control.water.mapper.JingxiafenzhanMapper;
import com.ruoyi.position.domain.Ryjz;
import com.ruoyi.position.domain.Ryss;
import com.ruoyi.position.mapper.RyjzMapper;
import com.ruoyi.position.mapper.RyssMapper;
import com.ruoyi.pressure.domain.KyLaneDblc;
import com.ruoyi.pressure.domain.KyLaneDblcReal;
import com.ruoyi.pressure.mapper.KyLaneDblcMapper;
import com.ruoyi.pressure.mapper.KyLaneDblcRealMapper;
import com.ruoyi.safety.domain.*;
import com.ruoyi.safety.dto.MarkDto;
import com.ruoyi.safety.dto.SafetyPointInfoDto;
import com.ruoyi.safety.dto.SafetyPointRealTimeDto;
import com.ruoyi.safety.mapper.*;
import com.ruoyi.safety.vo.MarkVo;
import com.ruoyi.tube.domain.Sgcdss;
import com.ruoyi.tube.mapper.SgcddyMapper;
import com.ruoyi.tube.mapper.SgcdssMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.safety.service.IMarkService;

/**
 * cesiumService业务层处理
 *
 * @author ruoyi
 * @date 2024-11-07
 */
@Service
public class MarkServiceImpl extends ServiceImpl<MarkMapper, Mark> implements IMarkService //修改
{
    @Autowired
    private MarkMapper markMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SafetyPointInfoMapper safetyPointInfoMapper;

    @Autowired
    private SafetyPointRealTimeMapper safetyPointRealTimeMapper;

    @Autowired
    private SafetySubstationInfoMapper safetySubstationInfoMapper;

    @Autowired
    private SafetySubstationRealTimeMapper safetySubstationRealTimeMapper;

    @Autowired
    private KyLaneDblcRealMapper kyLaneDblcRealMapper;

    @Autowired
    private KyLaneDblcMapper kyLaneDblcMapper;

    @Autowired
    private JingxiafenzhanMapper jingxiafenzhanMapper;

    @Autowired
    private JingxiaMapper jingxiaMapper;

    @Autowired
    private RyjzMapper ryjzMapper;

    @Autowired
    private RyssHistoryMapper ryssHistoryMapper;

    @Autowired
    private RyssMapper ryssMapper;

    @Autowired
    private SgcdssHistoryMapper sgcdssHistoryMapper;

    @Autowired
    private SgcdssMapper sgcdssMapper;

    @Autowired
    private SgcddyMapper sgcddyMapper;

    /**
     * 查询cesium
     *
     * @param id cesium主键
     * @return cesium
     */
    @Override
    public Mark selectMarkById(Long id) {
        return markMapper.selectMarkById(id);
    }

    /**
     * 查询cesium列表
     *
     * @param mark cesium
     * @return cesium
     */
    @Override
    public List<Mark> selectMarkList(Mark mark) {
        return markMapper.selectMarkList(mark);
    }

    /**
     * 新增cesium
     *
     * @param mark cesium
     * @return 结果
     */
    @Override
    public int insertMark(Mark mark) {
        return markMapper.insertMark(mark);
    }

    /**
     * 修改cesium
     *
     * @param mark cesium
     * @return 结果
     */
    @Override
    public int updateMark(Mark mark) {
        return markMapper.updateMark(mark);
    }

    /**
     * 批量删除cesium
     *
     * @param ids 需要删除的cesium主键
     * @return 结果
     */
    @Override
    public int deleteMarkByIds(Long[] ids) {
        return markMapper.deleteMarkByIds(ids);
    }

    /**
     * 删除cesium信息
     *
     * @param id cesium主键
     * @return 结果
     */
    @Override
    public int deleteMarkById(Long id) {
        return markMapper.deleteMarkById(id);
    }


    @Override
    public List<Mark> selectAll(Mark mark) {
        return markMapper.selectAll(mark);
    }

    @Override
    public List<Mark> listByType(String markType) {
        return markMapper.listByType(markType);
    }

    @Override
    public void updateNameById(Mark mark) {
        Long markId = mark.getId();
        Mark markById = markMapper.selectMarkById(markId);
        markById.setMarkName(mark.getMarkName());
        markMapper.updateMark(markById);
        return;
    }

    @Override
    public void deviceToPoint(MarkVo vo) {
        String deviceId = vo.getDeviceId();
        Device device = deviceMapper.selectDeviceByDeviceId(deviceId);
        SafetyPointInfoDto safetyPointInfoDto = safetyPointInfoMapper.selectSafetyPointInfoByPointCodeAndSubstationCode(device.getRedlineId(), device.getCameraStatus());

        SafetyPointRealTimeDto safetyPointRealTimeDto = new SafetyPointRealTimeDto();
//        Mark mark = markMapper.selectMarkByDeviceId(deviceId);
        Mark mark = markMapper.selectMarkById(Long.valueOf(vo.getPointId()));

        if (Objects.nonNull(safetyPointInfoDto)) {
            BeanUtils.copyProperties(safetyPointInfoDto, safetyPointRealTimeDto);
            String pointCode = safetyPointInfoDto.getPointCode();
            if (Objects.nonNull(pointCode)) {
                //已关联
                SafetyPointInfo safetyPointInfo = safetyPointInfoMapper.selectSafetyPointInfoByPointCode(pointCode);
                safetyPointInfo.setExtend2(vo.getPointId());
                safetyPointInfoMapper.updateSafetyPointInfo(safetyPointInfo);


                SafetyPointRealTime safetyPointRealTime = safetyPointRealTimeMapper.selectSafetyPointRealTimeByPointCode(pointCode);
                if (Objects.nonNull(safetyPointRealTime)) {
                    BeanUtils.copyProperties(safetyPointRealTime, safetyPointRealTimeDto);
                }
            }
//            System.out.println("===============================");
//            System.out.println(safetyPointRealTimeDto);
            mark.setCode(safetyPointRealTimeDto.getPointCode());
        }


        if (Objects.nonNull(safetyPointRealTimeDto)) {
            //null

            mark.setExtend1(safetyPointRealTimeDto.getSensorTypeName());
            mark.setInstallPosition(vo.getInstallAdress());
            mark.setDeviceId(deviceId);
            mark.setStatus(safetyPointRealTimeDto.getPointStatus());
//            mark.setDataTime(safetyPointRealTimeDto.getUploadTime());
            mark.setDataTime(safetyPointRealTimeDto.getUpdateTime());
            mark.setValue1(safetyPointRealTimeDto.getPointValue());
            mark.setUnit1(safetyPointRealTimeDto.getPointValueUnit());

            markMapper.updateMark(mark);
        }


        return;
    }

    @Override
    public void pointToPoint(MarkVo vo) {
        String pointCode = vo.getPointCode();
        String pointId = vo.getPointId();
//        String sensorTypeName = vo.getSensorTypeName();
        if (Objects.nonNull(pointId)) {
            Mark mark = markMapper.selectMarkById(Long.valueOf(pointId));
            if (Objects.nonNull(pointCode)) {
                SafetyPointInfo safetyPointInfo = safetyPointInfoMapper.selectSafetyPointInfoByPointCode(pointCode);
                safetyPointInfo.setExtend2(vo.getPointId());
                safetyPointInfoMapper.updateSafetyPointInfo(safetyPointInfo);

//                mark.setExtend3(sensorTypeName);
                String substationCode = safetyPointInfo.getSubstationCode();
                if (Objects.nonNull(substationCode)) {
                    SafetyPointRealTime safetyPointRealTime = safetyPointRealTimeMapper.selectSafetyPointRealTimeByPointCode(pointCode);
                    mark.setExtend2(substationCode);
                    mark.setCode(pointCode);
                    mark.setInstallPosition(safetyPointRealTime.getPointInstallLocation());
                    mark.setExtend1(safetyPointRealTime.getSensorTypeName());
                    mark.setValue1(safetyPointRealTime.getPointValue());
                    mark.setUnit1(safetyPointRealTime.getPointValueUnit());
                    mark.setStatus(safetyPointRealTime.getPointStatus());
//                    mark.setDataTime(safetyPointRealTime.getUploadTime());
                    mark.setDataTime(safetyPointRealTime.getUpdateTime());
                    markMapper.updateMark(mark);
                }
            }
        }
        return;
    }

    @Override
    public List<MarkDto> getSubPointList(MarkVo vo) {
        //查询分站  ===>  测点
        String substationCode = vo.getTypeBtnParam();

        //测点基本信息列表
        List<SafetyPointInfo> safetyPointInfoList = safetyPointInfoMapper.getSafetyPointInfoListBySubstationCode(substationCode);

        //过滤出已关联的测点信息  extend2
        List<SafetyPointInfo> filteredList = safetyPointInfoList.stream()
                .filter(safetyPointInfo -> safetyPointInfo.getExtend2() != null && !safetyPointInfo.getExtend2().isEmpty())
                .collect(Collectors.toList());

//        System.out.println(filteredList);

        //封装对象
        List<MarkDto> collect = filteredList.stream().map(objInList -> {
            String pointCode = objInList.getPointCode();
            SafetyPointRealTime safetyPointRealTime = safetyPointRealTimeMapper.selectSafetyPointRealTimeByPointCode(pointCode);
            MarkDto dto = new MarkDto();
            dto.setStatus(safetyPointRealTime.getPointStatus());
            dto.setValue1(safetyPointRealTime.getPointValue());
            dto.setUnit1(safetyPointRealTime.getPointValueUnit());
            dto.setExtend1(safetyPointRealTime.getSensorTypeName());
//            dto.setDataTime(safetyPointRealTime.getUploadTime());
            dto.setDataTime(safetyPointRealTime.getUpdateTime());
            dto.setInstallPosition(safetyPointRealTime.getPointInstallLocation());

            SafetyPointInfo safetyPointInfo = safetyPointInfoMapper.selectSafetyPointInfoByPointCode(pointCode);
            dto.setId(Long.valueOf(safetyPointInfo.getExtend2()));
            return dto;
        }).collect(Collectors.toList());
        return collect;


//        List<SafetySubstationRealTime> selectSafetySubstationRealTimeList = safetySubstationRealTimeMapper.selectSafetySubstationRealTimeList(null);
//
////        new ArrayList<>()
//        List<String> pointCodes = selectSafetySubstationRealTimeList.stream().map(safetySubstationRealTime -> {
//                    String substationCode = safetySubstationRealTime.getSubstationCode();
//                    List<SafetyPointInfo> safetyPointInfoList = safetyPointInfoMapper.getSafetyPointInfoListBySubstationCode(substationCode);
//
//                    return safetyPointInfoList.stream() // 将每个SafetyPointInfo列表转换为一个Stream
//                            .map(SafetyPointInfo::getPointCode) // 提取每个SafetyPointInfo的point_code
//                            .collect(Collectors.toList()); // 将这些point_code收集到一个列表中
//
//                }).flatMap(List::stream) // 将所有包含point_code的列表合并成一个Stream
//                .collect(Collectors.toList()); // 最终收集所有的point_code到一个列表中


//        return null;
    }

    @Override
    public List<Mark> listByExtend2(String extend2) {
        return markMapper.listByExtend2(extend2);
    }

    @Override
    public List<Mark> listByExtend1(String extend1) {
        return markMapper.listByExtend1(extend1);
    }

    @Override
    public void pointToKyPoint(MarkVo vo) {
        String pointId = vo.getPointId();

        Mark mark = markMapper.selectMarkById(Long.valueOf(pointId));
        if (Objects.nonNull(mark)) {
            String laneDblcId = vo.getLaneDblcId();
            KyLaneDblc kyLaneDblc = kyLaneDblcMapper.selectKyLaneDblcByLaneDblcId(laneDblcId);
            String measurementPointCode = kyLaneDblc.getMeasurementPointCode();
            if (Objects.nonNull(measurementPointCode)) {
                KyLaneDblcReal kyLaneDblcReal = kyLaneDblcRealMapper.selectKyLaneDblcRealByCode(measurementPointCode);
                if (Objects.nonNull(kyLaneDblcReal)) {
                    mark.setExtend3(laneDblcId);
                    mark.setValue2(kyLaneDblcReal.getSensorType());
                    mark.setInstallPosition(kyLaneDblcReal.getSensorLocation());

                    mark.setValue3(String.valueOf(kyLaneDblcReal.getQjdMonitorValue()));
                    mark.setUnit3(String.valueOf(kyLaneDblcReal.getSjdMonitorValue()));
                    mark.setDataTime(kyLaneDblcReal.getUpdateTime());

                    String measurementPointStatus = kyLaneDblcReal.getMeasurementPointStatus();
                    switch (measurementPointStatus) {
                        case "00000000":
                            mark.setStatus("正常");
                            break;
                        case "00000001":
                            mark.setStatus("报警");
                            break;
                        case "00000010":
                            mark.setStatus("负漂");
                            break;
                        case "00000100":
                            mark.setStatus("离线");
                            break;
                        case "00001000":
                            mark.setStatus("超量程");
                            break;
                        case "00010000":
                            mark.setStatus("分站故障");
                            break;
                        case "00100006":
                            mark.setStatus("不巡检");
                            break;
                        default:
                            break;
                    }

                    mark.setDelFlag("2");
                    markMapper.updateMark(mark);
                }
            }

        }

        return;
    }

    @Override
    public List<Mark> listByValue2(String value2) {
        return markMapper.listByValue2(value2);
    }

    @Override
    public void pointToSwSub(MarkVo vo) {
        String pointId = vo.getPointId();

        Mark mark = markMapper.selectMarkById(Long.valueOf(pointId));
        if (Objects.nonNull(mark)) {
            String autoID = vo.getAutoID();
            String code = vo.getCode();
            String channelNO = vo.getChannelNO();

            Jingxia jingxia = jingxiaMapper.getListByCodeAndNo(code, channelNO);
            if (Objects.nonNull(jingxia)) {
                mark.setValue1(String.valueOf(jingxia.getValueState()));
                mark.setUnit1(jingxia.getMeasureUnit());
                mark.setInstallPosition(jingxia.getAddress());
                mark.setDataTime(jingxia.getUDateTime());
                mark.setCode(jingxia.getCode());
                mark.setElasticStatus(jingxia.getMeasureName());

            }

            mark.setDelFlag("1");
            mark.setExtend3(autoID);
            markMapper.updateMark(mark);

        }

    }

    @Override
    public void pointToPersonSub(MarkVo vo) {
        String pointId = vo.getPointId();

        Mark mark = markMapper.selectMarkById(Long.valueOf(pointId));
        if (Objects.nonNull(mark)) {
            String baseStationCode = vo.getBaseStationCode();
            Ryjz ryjz = ryjzMapper.selectRyjzByBaseStationCode(baseStationCode);
            if (Objects.nonNull(ryjz)) {
                mark.setDelFlag("0");
                mark.setInstallPosition(ryjz.getLocationAnnotation());
                mark.setDataTime(ryjz.getDataTime());

                String count = ryssHistoryMapper.selectRyssHistoryByStationCode(baseStationCode);
                mark.setValue1(count);
            }

            mark.setExtend3(baseStationCode);
            markMapper.updateMark(mark);
        }
    }

    @Override
    public void pointToSg(MarkVo vo) {
        String pointId = vo.getPointId();

        Mark mark = markMapper.selectMarkById(Long.valueOf(pointId));
        if (Objects.nonNull(mark)) {
            String sgPointCode = vo.getSgPointCode();
            SgcdssHistory sgcdssHistory = sgcdssHistoryMapper.selectSgcdssHistoryByPointCode(sgPointCode);
            if (Objects.nonNull(sgcdssHistory)) {
                mark.setCode(sgPointCode);
                mark.setValue1(String.valueOf(sgcdssHistory.getPointValue()));
                mark.setUnit1(sgcdssHistory.getPointValueUnit());
                mark.setValue2(sgcdssHistory.getPointType());
                mark.setInstallPosition(sgcdssHistory.getPointArea());

            }
            mark.setDelFlag("4");
            markMapper.updateMark(mark);
        }

    }

    @Override
    public List<Mark> getListByType() {
        //点list
        List<Mark> markList = markMapper.getListByType();
        List<Mark> collect = markList.stream().map(mark ->
        {
            String delFlag = mark.getDelFlag();
            switch (delFlag) {
                case "0":
                    //人员定位
                    //基站编码
                    String ryjzCode = mark.getExtend3();
                    if (StringUtils.isNotEmpty(ryjzCode)) {
                        if (StringUtils.isNotEmpty(ryjzCode)) {
                            int ryssTotal = ryssHistoryMapper.selectRyssHistoryBySubstation(ryjzCode);
                            Ryjz ryjz = ryjzMapper.selectRyjzByBaseStationCode(ryjzCode);
                            mark.setValue1(String.valueOf(ryssTotal));
                            mark.setDataTime(ryjz.getDataTime());
                        }
                    }
                    break;
                case "1":
                    //水文
                    String jxfzId = mark.getExtend3();
                    if (StringUtils.isNotEmpty(jxfzId)) {
                        Jingxiafenzhan jingxiafenzhan = jingxiafenzhanMapper.selectJingxiafenzhanByAutoID(Long.valueOf(jxfzId));
                        if (Objects.nonNull(jingxiafenzhan)) {
                            Jingxia jingxia = jingxiaMapper.selectJingxiaByCode(jingxiafenzhan.getCode());
                            if (Objects.nonNull(jingxia)) {
                                mark.setValue1(jingxia.getValueState());
                                mark.setDataTime(jingxia.getUDateTime());
                            }
                        }
                    }
                    break;
                case "2":
                    //矿压
                    String kyId = mark.getExtend3();
                    if (StringUtils.isNotEmpty(kyId)) {
                        KyLaneDblc kyLaneDblc = kyLaneDblcMapper.selectKyLaneDblcByLaneDblcId(kyId);
                        if (Objects.nonNull(kyLaneDblc)) {
                            KyLaneDblcReal kyLaneDblcReal = kyLaneDblcRealMapper.selectKyLaneDblcRealByCode(kyLaneDblc.getMeasurementPointCode());
                            if (Objects.nonNull(kyLaneDblcReal)) {
                                mark.setValue1(String.valueOf(kyLaneDblcReal.getQjdMonitorValue()));
                                mark.setValue2(String.valueOf(kyLaneDblcReal.getSjdMonitorValue()));
                                mark.setDataTime(kyLaneDblcReal.getUpdateTime());
                            }
                        }
                    }
                    break;
                case "3":
                    //监控摄像头
                    break;
                case "4":
                    //束管测点
                    String sgCode = mark.getCode();
                    if (StringUtils.isNotEmpty(sgCode)) {
                        Sgcdss sgcdss = sgcdssMapper.selectSgcdssByPointCode(sgCode);
                        if (Objects.nonNull(sgcdss)) {
                            mark.setValue1(String.valueOf(sgcdss.getPointValue()));
                            mark.setDataTime(sgcdss.getUpdateTime());
                        }
                    }
                    break;
                case "5":
                    //安全  瓦斯
                    String pointCode = mark.getCode();
                    if (StringUtils.isNotEmpty(pointCode)) {
                        SafetyPointRealTime safetyPointRealTime = safetyPointRealTimeMapper.selectSafetyPointRealTimeByPointCode(pointCode);
                        if (Objects.nonNull(safetyPointRealTime)) {
                            mark.setValue1(safetyPointRealTime.getPointValue());
//                            mark.setDataTime(safetyPointRealTime.getUploadTime());
                            mark.setDataTime(safetyPointRealTime.getUpdateTime());
                        }
                    }
                    break;
                case "6":
                    //广播
                    break;

            }
            markMapper.updateMark(mark);
            return mark;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public List<Mark> getCheckBoxList(String[] ids) {
        return markMapper.getCheckBoxList(ids);
    }

    @Override
    public List<Ryss> getSubstationPersonDetails(Mark mark) {
        Long markId = mark.getId();
        Mark markById = markMapper.selectMarkById(markId);

        List<Ryss> personList = new ArrayList<>();
        if (Objects.nonNull(markById)){
            //基站编码
            String extend3 = markById.getExtend3();
            if (StringUtils.isNotEmpty(extend3)){
                personList = ryssMapper.getSubstationPersonDetails(extend3);
            }

        }
        return personList;
    }


    @Override
    public List<Mark> selectPersonList(Mark mark) {
        return markMapper.selectPersonList(mark);
    }

    @Override
    public List<Mark> selectSwList(Mark mark) {
        return markMapper.selectSwList(mark);
    }

    @Override
    public List<Mark> selectKyList(Mark mark) {
        return markMapper.selectKyList(mark);
    }


}


